import Vue from 'vue'

import Vuex from 'vuex'
import utils from "@/utils/uitls.js"
Vue.use(Vuex)

// 引入采购管理模块Vuex
import configSetting from '@/store/configSetting.js'
const store = new Vuex.Store({
    state: {
		branchId: '27'||'25'||'43', //分支机构id[关联分支机构表]
		bussinessId: '60'||'58'||'78', //商户id[关联商户表]
		accountId: '60'||'58'||'78', //账套id[关联账套表]
		code:'',//公众号授权code
		success:0,
		cType:0,//扫码从分享进详情
		commonityId:'',//商品spuid 扫码进入保存
		openId:uni.getStorageSync('updateShanghu') && JSON.parse(uni.getStorageSync('updateShanghu')).openId ? JSON.parse(uni.getStorageSync('updateShanghu')).openId : '',//商品分享进入 小程序openid
		gzhOpenId:'',//微信公众号openid 用于调用jssdk api使用
		//公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		navBarHeight:'',//导航栏高度
		statusBarHeight:'',//状态栏高度
		searchHeight:44,//搜索栏高度
		searchTop:'',//元素距离顶部的高度
		token:'',//token
		morentu:utils.xcxImgUrl()+'morentu.png',
		warehouseInfo:{ //分仓仓库
			warehouseName:'全部仓库',
			warehouseId:'',//分仓仓库id
		},
		searchName:'',//全局搜索
		searchArr:[],//搜索记录
		cainixihuanSearchParams:{},//猜你喜欢查询条件 首页保存 购物车页面使用
		skuInfo:{},//默认sku信息
		cardCommonity:[],//购物车商品
		activityInfo:{},//活动信息
		commonityItem:{},//单个商品订单信息
		catNum:'',
		cartCount:0
		
	},
	getters:{
		//针对于带搜索状态栏页面 返回具体搜索栏距离顶部的高度 
		top(state){
			// #ifdef H5
			return (state.searchTop*2) + 'rpx'
			// #endif
			// #ifndef H5
			return (state.statusBarHeight+state.navBarHeight+state.searchHeight)*2 + 'rpx'
			// #endif
		},
		//返回某个元素距离顶部的距离
		demoTopHieght(state){
			return (state.searchTop) + 'px'
		}
		
	},
    mutations: {
		//相当于同步的操作
		// 存储数量
		updateCount(state,val){
			state.cartCount = val
		},
		//修改分享进入的状态
		updateCType(state,val){
			state.cType = val
		},
		//存储公众号openid
		updateGzhOpenId(state,val){
			state.gzhOpenId = val
		},
		
		//存储商户信息
		updateShanghu(state,val){
			
			//授权code 进入
			
			// state.code = val.code
			// let shanghuState = JSON.parse(val.state)
			// if(shanghuState && shanghuState.length){
			// 	state.cType = shanghuState[0];
			// 	state.commonityId = shanghuState[1];
			// 	state.openId = shanghuState[2];
			// 	state.branchId = shanghuState[3];
			// 	state.accountId = shanghuState[4];
			// 	state.bussinessId = shanghuState[5];
			// 	console.log(shanghuState,state,'Store82');
			// }else{
			// 	state.branchId = shanghuState.branchId;
			// 	state.accountId = shanghuState.accountId;
			// 	state.bussinessId = shanghuState.bussinessId;
			// 	console.log(shanghuState,state,'Store82');
			// }
			
			//正常进入
			state.branchId = val.branchId;
			state.accountId = val.accountId;
			state.bussinessId = val.bussinessId;
			state.cType = val?.type || 0;
			state.commonityId = val?.commodityId || '';
			state.openId = val.openId;
		},
		
		//存储单个商品信息 用于跳转到订单详情回显
		updateCommonityItem(state,val){
			state.commonityItem = val
		},
		
		//存储活动信息
		updateActivityInfo(state,val){
			state.activityInfo = val
		},
		
		//存储购物车商品信息
		cardCommonity(state,val){
			state.cardCommonity = val
		},
		
		//存储默认信息
		updateSkuInfo(state,val){
			state.skuInfo = val
		},
		
		//存储首页猜你喜欢条件
		updateCainixihuanSearchParams(state,val){
			state.cainixihuanSearchParams = val
		},
		
		//存储头部搜索
		updateSearch(state,val){
			state.searchName = val
			if(val){
				//保存搜索记录到本地
				state.searchArr.unshift(val)
				if(state.searchArr.length > 10){
					state.searchArr.pop()
				}
				uni.setStorageSync('lsss',JSON.stringify(state.searchArr))
			}
		},
		//存储分仓仓库
		updateWarehouseInfo(state,val){
			state.warehouseInfo.warehouseId = val.warehouseId
			state.warehouseInfo.warehouseName = val.warehouseName
		},
		//存储token
		updateToken(state,val){
			state.token = val
		},
		// 赋值导航栏
		updateNavBarHeight(state,val){
			state.navBarHeight = val
		},
		//赋值状态栏
		updateStatusBarHeight(state,val){
			state.statusBarHeight = val
		},
		//赋值搜索栏
		updateSearchHeight(state,val){
			state.searchHeight = val 
		},
		// 赋值元素距离顶部的距离 不限制于搜索栏元素
		updateSearchTop(state,val){
			state.searchTop = val
		}
	},
    actions: {
		//相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
	},
	modules: {
		configSetting
	}
})
export default store
